/**
 * @file
 * @auth jinguangguo
 * @date 2017/1/18
 */

import Vue from 'vue';
import tpl from './tpl.html';
import commonService from '../../../common/commonService';
import service from './service'
import filter from '../../../../grsc-base/util/filter';
import lib from '../../../common/lib';
import Popover from '../../../../grsc-base/component/Popover.vue';
import Loading from '../../../../grsc-base/component/Loading.vue';
import NoData from '../../../../grsc-base/component/NoData.vue';
import Pager from '../../../../grsc-base/component/Pager.vue';
import Qrcode from '../../../../grsc-base/component/Qrcode.vue';
import MathTool from '../../../../grsc-base/util/mathtool';
import AlterPayPwd from '../component/alterPayPwd/main';
import TradePwd from '../component/tradePwd/main';
import setlogpwd from '../component/modifyLogPassword/modifyLogPassword'
import ResetPayPwd from '../component/resetPayPwd/main';
import resetGoogle from '../component/resetGoogle/main';
import toggleGoogle from '../component/toggleGoogle/main';
import closeGoogleDialog from '../component/closeGoogleDialog/main';
import setPayPwd from '../../../component/setPayPwd/main';
import setNickname from '../../../component/setNickName.vue';
import setPhone from '../../../component/setPhone.vue'
import transactionSet from '../component/TransactionSet/main'
import WeChat from '../component/WeChat/main'
import {
    REG_EMAIL,
    REG_PHONE
} from '../../../../grsc-base/common/constants'

const BASE_UID_NO = 100000;

export default Vue.extend({
    name: "Basic",
    template: tpl,
    components: {
        Popover,
        Qrcode,
        AlterPayPwd,
        TradePwd,
        ResetPayPwd,
        setlogpwd,
        resetGoogle,
        toggleGoogle,
        Loading,
        NoData,
        Pager,
        closeGoogleDialog,
        setPayPwd,
        setNickname,
        setPhone,
        transactionSet,
        WeChat
    },

    data() {
        return {

            REG_EMAIL,
            lib: lib,
            uid: '',
            weChatNo: '',
            accountBSD: '',
            Cincome: 0,
            Tincome: 0,
            Invite: "",
            email: '',
            ip: '',
            level: '',
            partner: '',
            member: '',
            ipList: null,
            limit: '',
            isSetPayPassword: false,
            nickname: '',
            mobile: '',
            typeList: [],
            isLoading: false,
            isIPLoading: true,
            isShowAddDialog: false,
            isAlterPayPwd: false,
            isTradePwd:false,
            isResetPayPwd: false,
            isResetGoogle: false,
            isSetPayPwd: false,
            isSettingNickname: false,
            isSettingPhone: false,
            isTransactionSet: false,
            states: false,
            isToggleGoogle: false, // 控制开关
            hasGoogleAuth: false, // 是否已经验证过
            openGoogleAuth: false, // 开启第一次谷歌验证
            isCloseGoogle: false, // 关闭谷歌验证
            isWeChat: false,
            type: 'wechat',
            bingding: false,
            dfpUsdt:0,
            condition: {
                pageNo: 1,
                pageSize: 10,
                pageNum: 1
            },
            param1: {
                assetCode: "DFP"
            }
        }
    },
    created() {
        this.realAuthDetection();
        this.fetchIP();
        this.fetchIPlog();
        this.queryGoogleStatus();
        this.UserState();
        this.getAccountType();
        this.getIncome();
        this.getInvite();
        this.getDfp();
    },
    mounted() {},
    methods: {
        //获取绑定类型
        getAccountType() {
            service.getAccountType().done(data => {
                if (data.code == '100200') {
                    this.typeList = data.data;
                } else {
                    window.error(data.msg)
                }
            })
        },
        //绑定微信
        weChat(name, type) {
            this.type = type;
            this.bingding = name ? true : false;
            this.isWeChat = true;
        },
        hideWeChat(data) {
            if (data != undefined && data != null) {
                this.realAuthDetection();
            }
            this.isWeChat = false;
        },
        hideIsTransactionSet(data) {
            if (data != undefined && data != null) {
                this.states = data;
            }
            this.isTransactionSet = false;
        },
        //检查是否开启免密支付
        UserState() {
            commonService.UserState().done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    this.states = rep.data.states == 0 ? false : true
                } else {
                    window.error(rep.msg)
                }
            }).fail((rep) => {
                window.error(rep.msg)
            })
        },

        //关闭时候无需输入密码即可关闭
        UserUpData() {
            let param = {
                states: this.states == false ? 1 : 0
            }

            let header = {
                'pay-password': ' '
            };
            service.UserUpDate(param, header).done((rep) => {
                if (rep.code == '100200') {
                    window.success(rep.msg)
                    this.states = rep.data.states == 1 ? true : false
                } else {
                    window.error(rep.msg)
                }
            }).fail((rep) => {
                window.error(rep.msg)
            })
        },
        //交易设置
        TransactionSet() {
            if (this.states == true || this.states == '1') {
                this.UserUpData();
                return false
            } else {
                this.isTransactionSet = true;
            }
        },
        getDfp() {
            service.getDfp().done(rep => {
                if (rep.code === window.CODE.SUCCESS) {
                    localStorage.setItem('dfpUsdt',rep.data.DFP)
                }
            })
        },

        //检验用户是否实名认证
        realAuthDetection() {
            this.isLoading = true;
            commonService.basic().done((basicRep) => {
                this.isLoading = false;
                if (basicRep.code === window.CODE.SUCCESS) {
                    let user = basicRep.data;
                    this.email = user.account;
                    this.uid = lib.getFormattedUid(user.uid);
                    this.weChatNo = user.weChatNo;
                    this.accountBSD = user.accountBSD;
                    this.level = user.authLevel;
                    this.partner = user.partner;
                    this.member = user.dfpMember;
                    this.isSetPayPassword = user.setPayPassword;
                    this.nickname = user.nickname;
                    this.mobile = user.mobile;
                    this.fetchWithdrawConfig();
                }
            });
        },

        fetchWithdrawConfig() {
            let key = '';
            switch (this.level) {
                case 'LEVEL0':
                    key = 'WITHDRAWLEVEL_0';
                    break;
                case 'LEVEL1':
                    key = 'WITHDRAWLEVEL_1';
                    break;
                case 'LEVEL2':
                    key = 'WITHDRAWLEVEL_2';
                    break;
            }


            commonService.assetConfigList({
                key
            }).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    let limitArr = [];
                    let noLimit = '';
                    let assetCode;
                    rep.data.forEach((item, index) => {
                        if (+item.profileValue > 0) {
                            assetCode = item.assetCode;
                            limitArr.push(item.profileValue + ' ' + assetCode);
                        } else if (+item.profileValue == 0) {
                            noLimit = ' , ' + this.$t('basic.noOtherlimit');
                        }
                    });

                    this.limit = limitArr.join(' + ') + noLimit;

                } else {
                    window.error(rep.msg)
                }
            })
        },


        /**
         * 获取当前ip
         * */
        fetchIP() {
            let param = {
                pageNo: 1,
                pageSize: 1
            };
            service.ipLog(param).done((rep) => {
                if (rep.code === window.CODE.SUCCESS && rep.data.list.length > 0) {
                    this.ip = rep.data.list[0].ipAddress;
                } else {
                    window.error(rep.msg)
                }
            })
        },

        /**
         * 获取ip地址历史
         * */
        fetchIPlog() {
            this.isIPLoading = true;
            let param = {
                pageNo: this.condition.pageNo,
                pageSize: this.condition.pageSize
            };
            service.ipLog(param).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    this.ipList = rep.data.list;
                    this.condition.pageNum = Number(filter.ceilFix(rep.data.total / this.condition.pageSize, 0));
                } else {
                    window.error(rep.msg)
                }
                this.isIPLoading = false;
            })
        },


        /**
         * 分页change
         * @param currentPage
         */
        changePage(currentPage) {
            this.condition.pageNo = currentPage;
            this.fetchIPlog();
        },

        copy(e) {
            lib.copy($(e));
          },
        showAlterPayPwd(val) {
            this.isAlterPayPwd = true;
        },
        showTradePwd(val) {
            this.isTradePwd = true;
        },
        showResetPayPwd() {
            this.isResetPayPwd = true;
        },
        changeShowStatus(val) {
            if (val == 'alterPayPwd') {
                this.isAlterPayPwd = false;
            } else if (val == 'resetPayPwd') {
                this.isResetPayPwd = false;
            } else if (val == 'tradePwd') {
                this.istradePwd = false;
            }
        },
        showAddDialog() {
            this.isShowAddDialog = true;
        },
        changeDigStatus() {
            this.isShowAddDialog = false;
        },

        hideSetSetNickname() {
            this.isSettingNickname = false;
            this.realAuthDetection();
        },
        hideTrade() {
            this.isTradePwd = false;
        },
        hideSetPhone() {
            this.isSettingPhone = false;
            this.realAuthDetection();
        },

        getIncome() {
            service.getIncome(this.param1).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                   
                    let dfpUsdt = localStorage.getItem('dfpUsdt');
                    this.Tincome = rep.data.todayProfit*this.dfpUsdt;
                    this.Cincome = rep.data.totalProfit*this.dfpUsdt;
                    
                } else {
                    window.error(rep.msg)
                }
            })
        },
        getInvite() {
            service.getInvite().done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    this.Invite = 'http://smt-ex.com/#/pages/register/register?code='+rep.data
                } else {
                    window.error(rep.msg)
                }
            })
        },

        /**
         * 谷歌验证码
         * */
        showResetGoogle() {
            this.isResetGoogle = true;
        },

        // 查询用户谷歌验证码开关状态
        queryGoogleStatus() {
            service.googleCodeQuery().done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    window.localStorage.setItem('GOOGLE_BOUND', rep.data.isBound);
                    window.localStorage.setItem('GOOGLE_STATUS', rep.data.status);

                    if (rep.data.isBound === false) { // 没有设置过谷歌验证码
                        this.isToggleGoogle = false;
                        return;
                    } else if (rep.data.isBound === true) {
                        this.hasGoogleAuth = true;
                        if (rep.data.status === 'OFF') { // 开关处于开启状态
                            this.isToggleGoogle = false;
                            return;
                        } else if (rep.data.status === 'ON') {
                            this.isToggleGoogle = true;
                        }
                    }
                } else {
                    window.error(rep.msg);
                }

            }).fail((rep) => {
                window.error(rep)
            })
        },

        // 切换谷歌验证码开关
        toggleGoogle() {
            // this.isToggleGoogle = !this.isToggleGoogle;
            if (this.isToggleGoogle) { // 当前是开启状态，要关闭谷歌验证
                this.updateGoogleStatusOff();
            } else { // 当前是关闭状态，要开启
                if (this.hasGoogleAuth) { // 已经验证过了谷歌验证码
                    this.updateGoogleStatusOn();
                } else { // 弹出谷歌验证页面
                    this.openGoogleAuth = true;
                }
            }
        },


        // 非首次开启时谷歌验证码时，用户开启谷歌验证码
        updateGoogleStatusOn() {
            let that = this;
            window.confirm({
                target: that.$refs.switch,
                content: that.$t('basic.securitySetting.openG'),
                confirmBtnText: that.$t('common.yes'),
                cancelBtnText: that.$t('common.no'),
                width: 180
            }).done(() => {
                service.googleCodeOn().done((rep) => {
                    if (rep.code === window.CODE.SUCCESS) {
                        //that.isToggleGoogle = false;
                        window.success(this.$t('common.success'));
                    } else {
                        window.error(rep.msg)
                    }
                    this.queryGoogleStatus();
                }).fail((rep) => {
                    window.error(rep)
                })
            }).fail(() => {

            });
        },

        // 非首次开启时谷歌验证码时，用户关闭谷歌验证码
        updateGoogleStatusOff() {
            this.isCloseGoogle = true;
        },


        //隐藏绑定谷歌绑定窗口
        closeGoogleAuth() {
            this.openGoogleAuth = false;
            this.queryGoogleStatus();
        },


        closeGoogleDialog() {
            this.isCloseGoogle = false;
            this.queryGoogleStatus();
        },


        setPayPwdSuccess() {
            this.isSetPayPassword = true;
        }

    },
    watch: {}
});